package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    //ES高级客户端
    @Resource
    private RestHighLevelClient restHighLevelClient;

    //ES低级客户端
    //@Resource
    //private TransportClient transportClient;

    //spring提供的ES高级客户端
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    //spring提供的ES低级客户端
    //@Resource
    //private ElasticsearchTemplate elasticsearchTemplate;

    @Resource
    private ListService listService;

    //创建索引库
    @GetMapping("/inner/creatIndex")
    public Result creatIndex(){

        //创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //添加映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //商品上架 添加索引
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }

    //商品下架 删除索引
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }

    //商品热度排名 增加热度
    @GetMapping("/inner/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }

    //商品搜索
    @PostMapping("/inner/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        SearchResponseVo vo = listService.search(searchParam);
        return vo;
    }
}
